// 获取应用实例
const app = getApp()
let API_URL = app.globalData.API_URL;
import { getAdvertise } from "../../request/api.js";
let iconUrl = 'https://atush.vip/static/icons/';

Page({
    data: {
        lang: null,
        langDate: null,
        active: 0,
        navigation: null,
        navTitle: "",
        isShow: false,
        addCont_type: false,
        tabs: [
            { id: 1, name: 'home', icon: iconUrl +'home.svg', fill_icon: iconUrl +'home-fill.svg' },
            { id: 2, name: 'infCircle', icon: iconUrl +'circle.svg', fill_icon: iconUrl +'circle-fill.svg' },
            { id: 3, name: '', icon: iconUrl +'add-con.svg', fill_icon: iconUrl +'add-con.svg' },
            { id: 4, name: 'shop', icon: iconUrl +'tabShop.svg', fill_icon: iconUrl +'tabShop-fill.svg' },
            { id: 5, name: 'userCentr', icon: iconUrl +'man.svg', fill_icon: iconUrl +'man-fill.svg' }
        ],
        select_list: [
            {id: 0, name: 'house', icon: iconUrl + 'oy.svg'},
            {id: 1, name: 'car', icon: iconUrl + 'car.svg' },
            {id: 2, name: 'jobs', icon: iconUrl + 'hizmet.svg' },
            {id: 3, name: 'usedGoods', icon: iconUrl +'nimKaxBuyum.svg' },
            {id: 4, name: 'delivery', icon: iconUrl +'paotui.svg' },
            {id: 5, name: 'experts', icon: iconUrl +'ailaSaymanRimunt.svg' },
            {id: 6, name: 'goodsPrice', icon: iconUrl +'malBaha.svg' },
            {id: 7, name: 'foundItem', icon: iconUrl +'tepiwalhinim.svg' },
            {id: 8, name: 'lostItem', icon: iconUrl +'yutturupKoyhan.svg' },
            {id: 9, name: 'service', icon: iconUrl +'sodaSetik.svg' }
        ],
        onReachBottomCount: 1,
        isPullDown: false,
        pullInitVal: 1,
        weatherNum: null,

        isHomeAd: true, // 首页满屏广告
        fitWhiteBg: true, // 进入小程序就显示的白色满屏背景
        second: 0,
        homePageAd: '',
        currLang: false, //
        fitAdUrl: '',

        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') - 40 +
            'px'
    },
    // 获取首页满屏广告
    getPost() {
        let currLangName = wx.getStorageSync('currLangName');
        if (currLangName) {
            this.setData({ currLang: true })
        }

        let data = { type: 2 }
        getAdvertise(res => {
            let request = res.data.data
            if (res.statusCode == 200) {
                this.setData({ homePageAd: request, second: request[0].date, fitAdUrl: API_URL + '/advertiseFiles/' + request[0].image_id + '/serve', weatherNum: request[0].weather })
                this.countdown();
            }
        }, data);
    },

    // 首页满屏广告倒计时
    countdown() {
        let that = this;
        var second = that.data.second
        if (second == 0) {
            that.setData({ isHomeAd: false, fitWhiteBg: false });
            return;
        } var time = setTimeout(function () {
            that.setData({ second: second - 1 });
            that.countdown();
        }
            , 1000)
    },

    // 关闭首页满屏广告
    closeHomeAdd() {
        this.setData({ isHomeAd: false, fitWhiteBg: false })
    },

    changeTabTo1() {
        this.setData({
            active: 1
        })
    },
    onChangeTab(e) {
        wx.pageScrollTo({
            scrollTop: 0,
            duration: 0
        })
        let item, index
        item = e.detail.item
        index = e.detail.index
        
        this.setData({ active: index })
        if (index == 0) {
            this.setData({ navigation: true })
        } else if (index == 1) {
            this.setData({ navigation: true })
        } else if (index == 2) {
            this.setData({ navigation: false })
        }
    },
    // 获取 ugLang
    getLang() {
        let data = wx.getStorageSync('currLangName')
        this.setData({ langDate: data })
    },
    homeIsPulldown(value) {
        this.setData({
            isPullDown: value
        })
    },
    freshIsPulldown(value) {
        this.setData({
            isPullDown: value
        })
    },
    onLoad(options) {
        this.setData({ navTitle: "infPlatform", navigation: true, lang: app.globalData.lang });
        this.getPost()
        this.getLang()
        if (options && options.activePage) {
            this.setData({ active: options.activePage })
        }
    },
    
    onPullDownRefresh: function () {
        if (this.data.activeTab == 0) {
            this.homeIsPulldown(true)
            this.setData({ pullInitVal: 2 })
            if (this.data.pullInitVal == 2) {
                this.homeIsPulldown(false)
                this.setData({ pullInitVal: 1 })
            }
        } else if (this.data.activeTab == 1) {
            this.freshIsPulldown(true)
            this.setData({ pullInitVal: 2 })
            if (this.data.pullInitVal == 2) {
                this.freshIsPulldown(false)
                this.setData({ pullInitVal: 1 })
            }
        } else {
            console.log("me page on pull down")
            wx.stopPullDownRefresh()
        }
    },
    onReachBottom() {
      if (this.data.active == 0) {
          this.setData({ onReachBottomCount: this.data.onReachBottomCount += 1 })
      }
      if (this.data.active == 1) {
          this.setData({ onReachBottomCount: this.data.onReachBottomCount += 1 })
      }
      if (this.data.active == 3) {
        this.setData({ onReachBottomCount: this.data.onReachBottomCount += 1 })
      }
    },

    // 转发
    onShareAppMessage: function (item) {
        let title = '';
        let id = 0;
        let index = 0;
        let sharePageNo = item.target.dataset.pageno;
        if (item.from == "button") {
            id = item.target.dataset.item.id;
            title = item.target.dataset.item.content_zh;
            if (item.target.dataset.index === 0) {
                index = 0;
            } else if (item.target.dataset.index > 0) {
                if (item.target.dataset.index % 5 === 0) {
                    index = 0;
                }
                if ((item.target.dataset.index - 1) % 5 === 0) {
                    index = 1;
                }
                if ((item.target.dataset.index - 2) % 5 === 0) {
                    index = 2;
                }
                if ((item.target.dataset.index - 3) % 5 === 0) {
                    index = 3;
                }
                if ((item.target.dataset.index - 4) % 5 === 0) {
                    index = 4;
                }
            }
        }
        return {
            title: title,
            path: "/pages/index/index?activePage=" + this.data.active + "&id=" + id + "&pageNo=" + sharePageNo + '&index=' + index
        }
    },

    // 朋友圈
    onShareTimeline: function () {
        return {
            title: '',
            query: "/pages/index/index",
        }
    }
})
